<template>
  <el-container class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside :width="sidebarWidth" class="sidebar">
      <!-- 用户信息区域 -->
      <div class="sidebar-user">
        <el-avatar
          :size="40"
          src="https://randomuser.me/api/portraits/men/78.jpg"
        />
        <div class="user-info">
          <div class="user-name">{{ userInfo.name }}</div>
          <div class="user-role">{{ userInfo.role }}</div>
        </div>
      </div>
      
      <el-divider />
      
      <!-- 导航菜单 -->
      <el-menu
        :default-active="$route.path"
        class="sidebar-menu"
        router
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-menu-item
          v-for="item in menuItems"
          :key="item.title"
          :index="item.to"
        >
          <i :class="item.icon"></i>
          <span slot="title">{{ item.title }}</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <!-- 顶部栏 -->
      <el-header class="header">
        <el-button
          type="text"
          class="mobile-menu-btn"
          @click="toggleSidebar"
        >
          <i class="el-icon-s-fold" v-if="!collapsed"></i>
          <i class="el-icon-s-unfold" v-else></i>
        </el-button>
        
        <div class="header-title">
          <span class="title-text">询标系统管理后台</span>
        </div>
        
        <div class="header-actions">
          <el-button
            type="text"
            class="logout-btn"
            @click="handleLogout"
          >
            <i class="el-icon-switch-button"></i>
            退出登录
          </el-button>
        </div>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main class="main-content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'AdminLayout',
  data() {
    return {
      collapsed: false,
      userInfo: {
        name: localStorage.getItem('userName') || '管理员',
        role: '系统管理员'
      },
      menuItems: [
        {
          title: '控制台',
          icon: 'el-icon-monitor',
          to: '/admin/dashboard'
        },
        {
          title: '询标列表',
          icon: 'el-icon-document',
          to: '/admin/inquiry-list'
        },
        {
          title: '询标记录',
          icon: 'el-icon-folder-opened',
          to: '/admin/inquiry-records'
        },
        {
          title: '操作日志',
          icon: 'el-icon-notebook-1',
          to: '/admin/inquiry-operation-log'
        },
        {
          title: '登录日志',
          icon: 'el-icon-user',
          to: '/admin/login-log'
        },
        {
          title: '查看日志',
          icon: 'el-icon-view',
          to: '/admin/view-log'
        },
        {
          title: '消息模板',
          icon: 'el-icon-message',
          to: '/admin/message-template'
        },
        {
          title: '参数配置',
          icon: 'el-icon-setting',
          to: '/admin/param-config'
        },
        {
          title: '统计分析',
          icon: 'el-icon-pie-chart',
          to: '/admin/statistics'
        }
      ]
    }
  },
  computed: {
    sidebarWidth() {
      return this.collapsed ? '64px' : '200px'
    }
  },
  methods: {
    toggleSidebar() {
      this.collapsed = !this.collapsed
    },
    
    handleLogout() {
      this.$confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('token')
        localStorage.removeItem('userRole')
        localStorage.removeItem('userName')
        this.$router.push('/login')
        this.$message.success('退出登录成功')
      }).catch(() => {
        this.$message.info('已取消退出')
      })
    }
  }
}

</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  transition: width 0.3s;
}

.sidebar-user {
  display: flex;
  align-items: center;
  padding: 20px 16px;
  color: #bfcbd9;
}

.user-info {
  margin-left: 12px;
  flex: 1;
}

.user-name {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 4px;
}

.user-role {
  font-size: 12px;
  color: #bfcbd9;
}

.sidebar-menu {
  border-right: none;
}

.sidebar-menu .el-menu-item {
  color: #bfcbd9;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #263445;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #409EFF;
  color: #fff;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.mobile-menu-btn {
  font-size: 18px;
  color: #666;
}

.header-title {
  flex: 1;
  margin-left: 20px;
}

.title-text {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
}

.header-actions {
  display: flex;
  align-items: center;
}

.logout-btn {
  color: #666;
  font-size: 14px;
}

.logout-btn:hover {
  color: #409EFF;
}

.main-content {
  background-color: #f0f2f5;
  padding: 0;
}

.content-wrapper {
  padding: 24px;
  min-height: calc(100vh - 60px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    z-index: 1000;
    height: 100vh;
  }
  
  .header-title {
    margin-left: 10px;
  }
  
  .title-text {
    font-size: 16px;
  }
  
  .content-wrapper {
    padding: 16px;
  }
}
</style>